//
//  IntentViewController.m
//  ExtensionUI
//
//  Created by Feng on 2021/7/28.
//

#import "IntentViewController.h"
#import "SceneControlIntent.h"


// As an example, this extension's Info.plist has been configured to handle interactions for INSendMessageIntent.
// You will want to replace this or add other intents as appropriate.
// The intents whose interactions you wish to handle must be declared in the extension's Info.plist.

// You can test this example integration by saying things to Siri like:
// "Send a message using <myApp>"

@interface IntentViewController ()
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;

@end

@implementation IntentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - INUIHostedViewControlling

// Prepare your view controller for the interaction to handle.
- (void)configureViewForParameters:(NSSet <INParameter *> *)parameters ofInteraction:(INInteraction *)interaction interactiveBehavior:(INUIInteractiveBehavior)interactiveBehavior context:(INUIHostedViewContext)context completion:(void (^)(BOOL success, NSSet <INParameter *> *configuredParameters, CGSize desiredSize))completion {
    // Do configuration here, including preparing views and calculating a desired size for presentation.
    
    if ([interaction.intentResponse isKindOfClass:[SceneControlIntentResponse class]]) {
        SceneControlIntentResponse *intentResponse = (SceneControlIntentResponse *)interaction.intentResponse;
        BOOL success = intentResponse.code != SceneControlIntentResponseCodeFailure;
        self.resultLabel.text = intentResponse.errorMsg;
        if (completion) {
            completion(success, parameters, [self desiredSize]);
        }
    }
    else {
        if (completion) {
            completion(YES, parameters, [self desiredSize]);
        }
    }
}

- (CGSize)desiredSize {
//    return [self extensionContext].hostedViewMaximumAllowedSize;
    CGFloat width = [self extensionContext].hostedViewMaximumAllowedSize.width;
    if (width == 0) {
        width = 320;
    }
    return CGSizeMake(width, 110);
}

@end
